import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/provider/base_request_form_provider.dart';
import 'package:x3_form_core/business_components/base_request_form_container.dart';

/// 底部单选
class BaseRequestFormSelectDialog extends StatefulWidget {

  final BaseHttpRequestModel request;

  final RequestSaveData requestSaveData;

  const BaseRequestFormSelectDialog({Key key, @required this.request, @required this.requestSaveData}) : super(key: key);

  @override
  _BaseRequestFormSelectDialogState createState() => _BaseRequestFormSelectDialogState();

}

class _BaseRequestFormSelectDialogState extends State<BaseRequestFormSelectDialog> {

  /// 入参
  BaseHttpRequestModel get _request => widget.request;
  BaseRequestFormProvider _baseRequestFormProvider;
  BaseBottomSheetController _baseBottomSheetController;

  @override
  void initState() {
    _baseBottomSheetController = BaseBottomSheetController(items: []);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _baseRequestFormProvider = BaseRequestFormProvider.of(context);
    _baseBottomSheetController.selectedItem = _baseRequestFormProvider.getDataSource(_request);
    _baseBottomSheetController.hit = widget.request.notes;
    return BaseRequestFormContainer(
      suffixIcon: BaseDropDownIcon(),
      showSuffixIcon: !_baseRequestFormProvider.isReadonly(widget.request),
      request: widget.request,
      onPress: _baseRequestFormProvider.isReadonly(_request) == true ? null : this._showBottomItems,
      child: this._buildBottomSheetSelectorWidget()
    );
  }

  Widget _buildBottomSheetSelectorWidget() {
    return BaseBottomSheetSelector(isIcon: false, controller: _baseBottomSheetController, disable: true, onChange: this._bottomSheetSelectorWidget);
  }

  void _bottomSheetSelectorWidget( dynamic value ) {
    _baseRequestFormProvider.setDataSource(_request, value.toJson());
    if (widget.requestSaveData != null) {
      widget.requestSaveData(widget.request, _baseRequestFormProvider.getMapDataSource(widget.request));
    }
  }

  void _showBottomItems() async {
    if (_baseBottomSheetController.items.length > 0) {
      _baseBottomSheetController.showBottomSheet();
      return;
    }
    if (_request.inputApi != null && _request.inputApi.isNotEmpty) {
      this._getBottomItems();
      return;
    }
    if (_request.dioX2Util != null && _request.dealWithResult != null) {
      this._getBottomItemsByFuture();
    }
  }

  void _getBottomItems() async {
    Map<String, dynamic> params = this._request.inputType == BaseHttpInputTypeEnum.LinkageSelectPost ? this._baseRequestFormProvider.getAllDataMapKey() : {};
    BaseHttpMethodEnum method = this._request.inputType == BaseHttpInputTypeEnum.LinkageSelectPost ? BaseHttpMethodEnum.POST : BaseHttpMethodEnum.GET;
    BaseComponentUtils.showLoadingDialog(context);
    BaseHttpResultModel result = await dioUtil.fetch(_request.inputApi, params: params, method: method);
    BaseComponentUtils.hideLoadingDialog(context);
    if ( result.success == false ) return;
    List<dynamic> items = result.data is List ? result.data : [];
    _baseBottomSheetController.items = items.map((e) => BaseBottomSheetSelectorModel(key: e['key'], value: e['value'])).toList();
    if (!widget.request.required) _baseBottomSheetController.items.insert(0, BaseBottomSheetSelectorModel(key: null, value: ''));
    _baseBottomSheetController.showBottomSheet();
  }

  void _getBottomItemsByFuture() async {
    BaseComponentUtils.showLoadingDialog(context);
    BaseHttpResultModel result = await _request.dioX2Util;
    BaseComponentUtils.hideLoadingDialog(context);
    List<Map<String, dynamic>> list = _request.dealWithResult(result);
    _baseBottomSheetController.items = list.map((e) => BaseBottomSheetSelectorModel(key: e['key'], value: e['value'])).toList();
    _baseBottomSheetController.showBottomSheet();
  }

}
